<?php
require_once('TVB/PWORKS/common/cache/ICacheHelper.iface.php');
require_once('TVB/PWORKS/mvc/IConfigHelper.iface.php');
require_once('TVB/PWORKS/mvc/tool/ConfChecker.class.php');
require_once('TVB/PWORKS/mvc/AppXMLCfgLoader.class.php');

class CachedConfigHelper implements IConfigHelper {
    
    /**
     * @var ICacheHelper
     */
    private $cacheHelper;
    private $appConf;

    public function setCacheHelper(ICacheHelper $cacheHelper) {
        $this->cacheHelper = $cacheHelper;
    }

    public function init($filename, $doCheck=false) {

        if(!$this->cacheHelper->fetch('app_conf_init_flag')){
            //Check xml file first

            //Loading...
            $loader = new AppXMLCfgLoader();
            $loader->load($filename);
             
            $this->appConf = $loader->getAppConfig();

            //check configuration
            if($doCheck){
                ConfChecker::check($this);
            }
             
            $this->cacheHelper->store('app_conf_init_flag', true);
            $this->cacheHelper->store('app_conf', $this->appConf);
        }
        else{
            $this->appConf = $this->cacheHelper->fetch('app_conf');
        }
    }

    public function getDefaultAction(){
        return $this->appConf->defaultAction;
    }

    public function getActionConfig($name) {
        if(is_array($this->appConf->actions) && array_key_exists($name, $this->appConf->actions))
        {
            return $this->appConf->actions[$name];
        }
        else{
            return null;
        }
    }

    public function getFilterConfig($name) {
        if(is_array($this->appConf->filters) && array_key_exists($name, $this->appConf->filters))
        {
            return $this->appConf->filters[$name];
        }
        else{
            return null;
        }
    }

    public function getGlobal($name) {
        if(is_array($this->appConf->globals) && array_key_exists($name, $this->appConf->globals))
        {
            return $this->appConf->globals[$name];
        }
        else{
            return null;
        }
    }

    public function getResultType($name) {
        if(is_array($this->appConf->resultTypes) && array_key_exists($name, $this->appConf->resultTypes))
        {
            return $this->appConf->resultTypes[$name];
        }
        else{
            return null;
        }
    }

    public function getApp(){
        return $this->appConf;
    }
    
   	//[2009-05-26] Implement Feature Request 2777528
	//----------------------------------------------
    public function getGlobalFilters(){
    	return $this->appConf->globalFilters;
    }
}
?>
